/*var mongoose=require('mongoose');
var config=require('../config');
var mongodb = require('mongodb');*/
var mongoose=require('./mongoConn');
var Schema=mongoose.Schema;
/*var db=mongoose.createConnection(config.db_url);
db.on('error',function(error){
    db.close();
    console.log(error);
});

db.on('close', function () {
    db.open(config.db_url);
    console.log("重新连接mongo");
});*/

var logSchema = new Schema({
    box_id: String,
    user: String, //操作人
    date: Date, //操作日期
    operation: String ,//操作内容
    opertype:String  //显示操作类型
});
/*console.log("进入log.js");
var Log = db.model("Log", logSchema);*/
var Log = mongoose.model("Log", logSchema);

Log.prototype.findDataBySim=function (sim,callback){
    Log.findOne({sim:sim}, function(data){
        return callback(data);
    });
}

Log.prototype.findAllSim=function(callback){
    Log.find(function(err,datas){
        return callback(datas);
    })
}

Log.prototype.findForPage=function(obj,callback){
    var q=obj.search||{};                                       //查询条件
    var col=obj.columns||{};                                    //返回字段
    var query=Log.find({opertype:null},col).skip(obj.page.skipFrom).limit(obj.page.limit);

    query.exec(function(err, datas) {
        if (err) {
            callback(err, null, null);
        } else {
            Log.count(q, function(err, count) {
                if (err) {
                    callback(err, null, null);
                } else {
                    var pageCount = Math.ceil(count / obj.page.limit);
                    callback(null,count,pageCount, datas);
                }
            });
        }
    });
}

Log.prototype.findOTForPage=function(obj,callback){
    var q=obj.search||{};                                       //查询条件
    var col=obj.columns||{};                                    //返回字段
    var query=Log.find({opertype:{$ne:null}},col).skip(obj.page.skipFrom).limit(obj.page.limit);

    query.exec(function(err, datas) {
        if (err) {
            callback(err, null, null);
        } else {
            Log.count({opertype:{$ne:null}}, function(err, count) {
                if (err) {
                    callback(err, null, null);
                } else {
                    var pageCount = Math.ceil(count / obj.page.limit);
                    callback(null,count,pageCount, datas);
                }
            });
        }
    });
}

module.exports = Log;